// 商家相关接口,包括新增商家,删除商家,查询商家,模糊查询商家
const express = require("express")
const router = express.Router()
// 引入连接池
const pool = require("../pool/pool")
// 引入封装后的响应
const Response = require("../utils/Response")
// 引入表单验证部分
const Joi = require("joi")

// 1.商家查询接口
router.get("/pet-business", async (req, res) => {
  // 表单验证
  let schema = Joi.object({
    page: Joi.number().required(),
    pagesize: Joi.number().integer().max(50).required(),
  })
  let { error, value } = schema.validate(req.query)
  if (error) {
    res.send(Response.error(error))
    return
  }
  // 执行查询数组
  try {
    let { page, pagesize } = req.query
    let startIndex = (page - 1) * pagesize
    let size = parseInt(pagesize)
    let sql = "select * from business limit ?,?"
    let result = await pool.querySync(sql, [startIndex, size])
    // 查询总数量
    let sql2 = "select count(*) as count from business"
    let result2 = await pool.querySync(sql2, [startIndex, size])
    let total = result2[0].count
    res.send(
      Response.ok({ page: parseInt(page), pagesize: size, total, result })
    )
  } catch (error) {
    res.send(Response.error(error))
  }
})

// 2.添加商家接口
router.post("/pet-business/add", (req, res) => {
  let {
    b_img,
    b_price,
    b_JS,
    b_socre,
    b_feature,
    b_city,
    b_city_detail,
    b_offer,
    b_name,
  } = req.body
  let schema = Joi.object({
    b_img: Joi.string().required(),
    b_price: Joi.number().required(),
    b_JS: Joi.number().required(),
    b_socre: Joi.required(),
    b_feature: Joi.string().required(),
    b_city: Joi.string().required(),
    b_city_detail: Joi.string().required(),
    b_offer: Joi.string().required(),
    b_name: Joi.string().required(),
  })
  let { error, value } = schema.validate(req.body)
  if (error) {
    res.send(Response.error(error))
    return
  }
  let sql = `insert into business values (?,?,?,?,?,?,?,?,?,?) `
  pool.query(
    sql,
    [
      ,
      b_img,
      b_price,
      b_JS,
      b_socre,
      b_feature,
      b_city,
      b_city_detail,
      b_offer,
      b_name,
    ],
    (err, result) => {
      if (err) {
        res.send(Response.error(500, err))
        throw err
      }
      res.send(Response.ok())
    }
  )
})
//4. 删除商家接口
router.post("/pet-business/del", (req, res) => {
  let { b_id } = req.body
  let schema = Joi.object({
    b_id: Joi.string().required(),
  })
  let { error, value } = schema.validate(req.body)
  if (error) {
    res.send(Response.error(400, error))
    return
  }
  let sql = "delete from business where b_id=?"
  pool.query(sql, [b_id], (error, result) => {
    if (error) {
      res.send(Response.error(500, error))
      throw error
    }
    res.send(Response.ok())
  })
})
// 3.模糊查询商家接口
router.post("/pet-business/name", (req, res) => {
  let { name } = req.body
  let schema = Joi.object({
    name: Joi.string().required(),
  })
  let { error, value } = schema.validate(req.body)
  if (error) {
    res.send(Response.error(400, error))
    return
  }
  let sql = "select * from business where b_name like ?"
  pool.query(sql, [`%${name}%`], (error, result) => {
    if (error) {
      res.send(Response.error(500, error))
      throw error
    }
    res.send(Response.ok(result))
  })
})

// 通过id查询电影接口
router.post("/pet-business/b_id", (req, res) => {
  let { b_id } = req.body
  let schema = Joi.object({
    b_id: Joi.number().required(),
  })
  let { error, value } = schema.validate(req.body)
  if (error) {
    res.send(Response.error(400, error))
    return
  }
  let sql = "select * from business where b_id=?"
  pool.query(sql, [b_id], (error, result) => {
    if (error) {
      res.send(Response.error(500, error))
      throw error
    }
    if (result && result.length == 0) {
      res.send(Response.ok(null))
    } else {
      res.send(Response.ok(result[0]))
    }
  })
})
// 修改商家接口
router.post("/pet-business/update", (req, res) => {
  let {
    b_img,
    b_price,
    b_JS,
    b_socre,
    b_feature,
    b_city,
    b_city_detail,
    b_offer,
    b_name,
    b_id,
  } = req.body
  let schema = Joi.object({
    b_id: Joi.number().required(),
    b_img: Joi.string().required(),
    b_price: Joi.number().required(),
    b_JS: Joi.number().required(),
    b_socre: Joi.required(),
    b_feature: Joi.string().required(),
    b_city: Joi.string().required(),
    b_city_detail: Joi.string().required(),
    b_offer: Joi.string().required(),
    b_name: Joi.string().required(),
  })
  let { error, value } = schema.validate(req.body)
  if (error) {
    res.send(Response.error(error))
    return
  }
  // 执行修改操作
  let sql = `update business set  b_img=?,b_price=?,b_JS=?,b_socre=?,b_feature=?,b_city=?,b_city_detail=?,b_offer=?,b_name=? where b_id=? `;
  pool.query(
    sql,
    [
      b_img,
      b_price,
      b_JS,
      b_socre,
      b_feature,
      b_city,
      b_city_detail,
      b_offer,
      b_name,
      b_id
    ],
    (err, result) => {
      if (err) {
        res.send(Response.error(500, err))
        throw err
      }
      res.send(Response.ok())
    }
  )
})
module.exports = router
